<template>
  <el-dialog
    title="实操作业"
    :close-on-click-modal="false"
    append-to-body
    :visible.sync="visible"
    class="jsbos-dialog jsbos-dialog_center"
    lock-scroll
    width="800px"
  >
    <el-row :gutter="15" class>
      <el-form
        ref="elForm"
        :model="dataForm"
        :rules="rules"
        size="small"
        label-width="100px"
        label-position="right"
      >
        <template v-if="!loading">
          <el-col :span="24">
            {{ homework.name }}
          </el-col>
          <el-col :span="24">
            <h5 class="border-title" data-v-9684a774="">作业要求</h5>
          </el-col>
          <el-col :span="24">
            <p>{{ homework.description }}</p>
            <UploadFz
              v-model="homework.attachments"
              disabled
              detailed
              :fileSize="5"
              sizeUnit="MB"
              :limit="5"
              buttonText="点击上传"
            >
            </UploadFz>
          </el-col>
          <el-col :span="24"> </el-col>

          <el-col :span="24">
            <h5 class="border-title" data-v-9684a774="">作业内容</h5>
          </el-col>
          <el-col :span="24">
            <el-input
              v-model="dataForm.description"
              placeholder="请输入作业内容"
              :rows="3"
              true
              type="textarea"
            ></el-input>
          </el-col>
          <el-col :span="24">
            <UploadFz
              v-model="dataForm.attachments"
              :fileSize="50"
              sizeUnit="MB"
              :limit="5"
              buttonText="点击上传"
            ></UploadFz>
          </el-col>
        </template>
      </el-form>
    </el-row>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取 消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" v-if="!isDetail"
        >确 定</el-button
      >
    </span>
  </el-dialog>
</template>
<script>
import request from "@/utils/request";

export default {
  components: {},
  props: [],
  data() {
    return {
      visible: false,
      loading: false,
      isDetail: false,
      dataForm: {
        homeworkId: "",
        courseId: "",
        description: "",
        attachments: [],
        enabledMark: "",
        creatorUserId: "",
        creatorTime: "",
      },
      homework: {
        courseId: "",
        name: "",
        description: "",
        attachments: [],
        completeType: "submit",
      },
      rules: {},
      enabledMarkOptions: [
        { fullName: "未提交", id: "0" },
        { fullName: "已提交", id: "1" },
        {
          fullName: "已批阅",
          id: "2",
        },
      ],
    };
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    clearData(data) {
      for (let key in data) {
        if (data[key] instanceof Array) {
          data[key] = [];
        } else if (data[key] instanceof Object) {
          this.clearData(data[key]);
        } else {
          data[key] = "";
        }
      }
    },
    init(data, isDetail) {
      data.attachments = JSON.parse(data.attachments);
      this.homework = data;
      this.visible = true;
      this.isDetail = isDetail || false;
      this.$nextTick(() => {
        this.$refs["elForm"].resetFields();
        this.dataForm.homeworkId = data.id;
        this.dataForm.courseId = data.courseId;
        this.dataForm.studyId = data.studyId;
      });
      this.$store.commit("generator/UPDATE_RELATION_DATA", {});
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["elForm"].validate((valid) => {
        if (valid) {
          this.request();
        }
      });
    },
    request() {
      var _data = this.dataList();
      if (!this.dataForm.id) {
        request({
          url: "/api/course/TemsCourseStudyHomework",
          method: "post",
          data: _data,
        }).then((res) => {
          this.$message({
            message: res.msg,
            type: "success",
            duration: 1000,
            onClose: () => {
              this.visible = false;
              this.$emit("refresh", true);
            },
          });
        });
      } else {
        request({
          url: "/api/course/TemsCourseStudyHomework/" + this.dataForm.id,
          method: "PUT",
          data: _data,
        }).then((res) => {
          this.$message({
            message: res.msg,
            type: "success",
            duration: 1000,
            onClose: () => {
              this.visible = false;
              this.$emit("refresh", true);
            },
          });
        });
      }
    },
    dataList() {
      var _data = JSON.parse(JSON.stringify(this.dataForm));
      _data.attachments = JSON.stringify(_data.attachments);
      return _data;
    },
    dataInfo(dataAll) {
      let _dataAll = dataAll;
      _dataAll.attachments = JSON.parse(_dataAll.attachments);
      this.dataForm = _dataAll;
    },
  },
};
</script>
<style>
.border-title {
  margin: 10px 0;
  font-size: 14px;
  line-height: 20px;
  padding-left: 10px;
  position: relative;
}
.border-title::before {
  content: "";
  width: 3px;
  height: 14px;
  background-color: #268df7;
  position: absolute;
  left: 0;
  top: 3px;
}
</style>
